require.config({
  paths: {
    jquery: 'libs/jquery-1.11.1.min',
    underscore: 'libs/underscore-min',
    backbone: 'libs/backbone-min'
  }

});
require([
	'jquery'
], function($){
		require([
		  "libs/jquery-ui-1.10.4.custom.min", "libs/JResponsive-v1.5"
		], function(){
			require(['https://apis.google.com/js/client.js?onload=init']);
		});
		require(["libs/macSearch"]);
});

function init() {
	var googleApiModule = function(){
		  var token;
		  var clientId = '839470061072-vec92qsr1asm8donau9upgbg1bgmf3tv.apps.googleusercontent.com';
		  var scopes = 'https://www.googleapis.com/auth/plus.me https://picasaweb.google.com/data/';
		  function checkAuth() {
			gapi.auth.authorize({client_id: clientId, scope: scopes, immediate: true}, handleAuthResult);
		  };
		  function handleAuthResult(authResult) {
			var unauthorizedBlock = document.getElementById('unauthorized');
			if (authResult && !authResult.error) {
				  unauthorizedBlock.style.display = 'none';
					document.getElementById('authorized').style.display = 'block';
				  
				  token = gapi.auth.getToken();
				  $.ajaxSetup({
						/*//not working 
							headers: [{ 'GData-Version': '2' }, {'Authorization' : 'Bearer ' + token.access_token}],*/
						  data : {
							alt : 'json', access_token : token.access_token, v : 2
						  }
					});
				$( "#tabs" ).tabs({
					activate: function(event, ui) {
						window.location.hash = ui.newPanel.attr('id');
						$('body').css('height', '');
						//$(this).css('height', 'auto');
					//	$(this).css('overflow', 'visible');
					},
					//fx: { opacity: 'toggle' },
					beforeActivate: function(event, ui) {
						//$(this).css('height', $(this).height());
					//	$(this).css('overflow', 'hidden');
					}
				});
				require([
					'app'
					], function(app){
						$('#templates').load('templates.html', function(){app.initialize();});
				  
				});
			} else {
				unauthorizedBlock.style.display = 'block';
				var authorizeButton = document.getElementById('authorize-button');
				authorizeButton.onclick = handleAuthClick;
			}
		  };
		  function handleAuthClick(event) {
			gapi.auth.authorize({client_id: clientId, scope: scopes, immediate: false}, handleAuthResult);
			return false;
		  };
		return function() {
			window.setTimeout(checkAuth,1);
		};
	};
	googleApiModule()();
}
